package com.itcast.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itcast.pojo.Admin;
import com.itcast.pojo.Admin1;
import com.itcast.pojo.Perms;
import com.itcast.service.Admin1service;
import com.itcast.service.Adminservice;
import com.itcast.service.RoleService;
import com.itcast.shiro.salt.MySimpleByteSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
@Slf4j
public class CustomerRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
        log.info("进入授权方法=====");
        System.out.println("==========授权==========");
        Admin1service admin1service = (Admin1service) ApplicationContextHolder.getBean("admin1service");
        RoleService roleService = (RoleService) ApplicationContextHolder.getBean("roleService");
        List<Admin1> admins = admin1service.queryRoleByName(primaryPrincipal);
        if(!CollectionUtils.isEmpty(admins)){
            for (Admin1 admin1 : admins) {
                SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
                admin1.getRole().forEach(role -> {
                    log.info("-------------[{}]",role.getName());
                    simpleAuthorizationInfo.addRole(role.getName());
                    List<Perms> perms = roleService.queryPermsById(role.getId());
                    if(!CollectionUtils.isEmpty(perms)){
                        perms.forEach(perm -> {
                            simpleAuthorizationInfo.addStringPermission(perm.getName());
                        });
                    }
                });
                return simpleAuthorizationInfo;
            }


        }
        return null;
    }


    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取用户名
        String principal = (String) authenticationToken.getPrincipal();
        log.info(principal);
        Adminservice adminservice = (Adminservice) ApplicationContextHolder.getBean("adminservice");
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("adminName",principal);
        Admin admin = adminservice.getOne(queryWrapper);
        if(!ObjectUtils.isEmpty(admin)){
            return new SimpleAuthenticationInfo(principal,admin.getPassword(), new MySimpleByteSource(admin.getSalt()),this.getName());
        }
        return null;
    }
}
